package com.wordsay.analyer.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.lionsoul.jcseg.core.IWord;

import com.wordsay.analyer.Analyer;
import com.wordsay.comparator.CountComparator;
import com.wordsay.comparator.TalkTimeComparator;
import com.wordsay.comparator.TalkWordCountComparator;
import com.wordsay.context.Context;
import com.wordsay.context.Resource;
import com.wordsay.entity.GroupDialog;
import com.wordsay.entity.HotItem;
import com.wordsay.entity.OverallAnalystResult;
import com.wordsay.entity.PersonDialog;
import com.wordsay.entity.Talk;
import com.wordsay.util.DateTimeUtil;

/**
 * 基本信息分析器
 * @author squirrelrao
 *
 */
public class OverallAnalyer implements Analyer {

	@Override
	public Object execute(Object target) throws Exception {

		Resource resource = (Resource)target;
		
		OverallAnalystResult analystResult = new OverallAnalystResult();
		
		long start = System.currentTimeMillis();
		
		//统计dialogs(person,dicussion,group)
		this.analystDialogs(analystResult, resource);
		
		System.out.println("analyst dialogs and talking persons spent:"+(System.currentTimeMillis()-start)+"ms");
		start = System.currentTimeMillis();
		
		//统计hot chat period,day
		this.analystHotPeriod(analystResult, resource);
		
		System.out.println("analyst hot period spent:"+(System.currentTimeMillis()-start)+"ms");
		start = System.currentTimeMillis();
		
		//统计talks
		this.analystTalks(analystResult, resource);
		
		System.out.println("analyst talks spent:"+(System.currentTimeMillis()-start)+"ms");
		start = System.currentTimeMillis();

		//统计hot words
		this.analystHotWords(analystResult, resource);
				
	    System.out.println("analyst hotWords spent:"+(System.currentTimeMillis()-start)/1000+"s");				
				
		return analystResult;
		
	}
	
	
	/**
	 * analyst the hot words
	 * @param analystResult
	 * @param resource
	 */
	private void analystTalks(OverallAnalystResult analystResult,Resource resource) {
		
		List<Talk> allTalks = new ArrayList<Talk>();
		List<String> personDialogs = new ArrayList<String>();
		Iterator<PersonDialog> dialogs = resource.getPersonDialogs().values().iterator();
		while(dialogs.hasNext()){
			
			PersonDialog dialog = dialogs.next();
			allTalks.addAll(dialog.getTalks());
		}
		
		Iterator<GroupDialog> gDialogs = resource.getGroupDialogs().values().iterator();
		while(gDialogs.hasNext()){
			
			GroupDialog dialog = gDialogs.next();
			allTalks.addAll(dialog.getTalks());
		}
		
		Iterator<GroupDialog> dDialogs = resource.getDiscussionDialogs().values().iterator();
		while(dDialogs.hasNext()){
			
			GroupDialog dialog = dDialogs.next();
			allTalks.addAll(dialog.getTalks());
		}
		
		Comparator talkTimeComparator = new TalkTimeComparator();
		Comparator talkWordCountComparator = new TalkWordCountComparator();
		
		Collections.sort(allTalks,talkTimeComparator);
		analystResult.setFirstTalk(allTalks.get(0));
		analystResult.setLastTalk(allTalks.get(allTalks.size()-1));
		
		Collections.sort(allTalks,talkWordCountComparator);
		analystResult.setMostWordsTalk(allTalks.get(0));
	}
	
	
	/**
	 * analyst the hot words
	 * @param analystResult
	 * @param resource
	 */
	private void analystHotWords(OverallAnalystResult analystResult,Resource resource) {
		
		long totalWordCount = 0;
		long totalTalkCount = 0;
		List<String> personDialogs = new ArrayList<String>();
		Iterator<PersonDialog> dialogs = resource.getPersonDialogs().values().iterator();
		List<HotItem> hotWords = new ArrayList<HotItem>();
		while(dialogs.hasNext()){
			
			PersonDialog dialog = dialogs.next();
			List<Talk> talks = dialog.getTalks();
			for(Talk talk : talks){
				
				totalWordCount += talk.getIwords().size();
				totalTalkCount++;
				this.analystHotWords(talk, hotWords);
			}
		}
		
		Iterator<GroupDialog> gDialogs = resource.getGroupDialogs().values().iterator();
		while(gDialogs.hasNext()){
			
			GroupDialog dialog = gDialogs.next();
			List<Talk> talks = dialog.getTalks();
			for(Talk talk : talks){
				
				totalWordCount += talk.getIwords().size();
				totalTalkCount++;
				this.analystHotWords(talk, hotWords);


			}
		}
		
		Iterator<GroupDialog> dDialogs = resource.getDiscussionDialogs().values().iterator();
		while(dDialogs.hasNext()){
			
			GroupDialog dialog = dDialogs.next();
			List<Talk> talks = dialog.getTalks();
			for(Talk talk : talks){
				
				totalWordCount += talk.getIwords().size();
				totalTalkCount++;
				this.analystHotWords(talk, hotWords);

			}
		}
		
		analystResult.setTotalTalkCount(totalTalkCount);
		analystResult.setTotalWordsCount(totalWordCount);
		
		Collections.sort(hotWords, new CountComparator());
		analystResult.setTopHotWords(hotWords.subList(0,20));
	}
	
	/**
	 * analyst hot period
	 * @param analystResult
	 * @param resource
	 */
	private void analystHotPeriod(OverallAnalystResult analystResult,Resource resource) {
		
		List<String> personDialogs = new ArrayList<String>();
		Iterator<PersonDialog> dialogs = resource.getPersonDialogs().values().iterator();
		
		List<HotItem> timePeriods = new ArrayList<HotItem>();
		List<HotItem> weekDays = new ArrayList<HotItem>();
		List<HotItem> hotDays = new ArrayList<HotItem>();

		while(dialogs.hasNext()){
			
			PersonDialog dialog = dialogs.next();
			this.analystPeriodTime(dialog.getTalks(), timePeriods, weekDays,hotDays);
		}
		
		Iterator<GroupDialog> gDialogs = resource.getGroupDialogs().values().iterator();
		while(gDialogs.hasNext()){
			
			GroupDialog dialog = gDialogs.next();
			this.analystPeriodTime(dialog.getTalks(), timePeriods, weekDays,hotDays);
			
		}
		
		Iterator<GroupDialog> dDialogs = resource.getDiscussionDialogs().values().iterator();
		while(dDialogs.hasNext()){
			
			GroupDialog dialog = dDialogs.next();
			this.analystPeriodTime(dialog.getTalks(), timePeriods, weekDays,hotDays);			
		}
		
		Comparator comparator = new CountComparator();
		Collections.sort(timePeriods, comparator);
		Collections.sort(weekDays,comparator);
		Collections.sort(hotDays,comparator);
		
		analystResult.setHotChatPeriod(timePeriods);
		analystResult.setHotChatWeekDay(weekDays);
		analystResult.setHotDay(hotDays);
		
	}
	
	
	private void analystPeriodTime(List<Talk> talks,List<HotItem> timePeriods,List<HotItem> weekDays,List<HotItem> hotDays){
		
		for(Talk talk : talks){
			
			HotItem timePeriod = new HotItem(DateTimeUtil.getHour(talk.getTime()),talk.getIwords().size());
			int indexOf = -1;
            if((indexOf = timePeriods.indexOf(timePeriod) ) != -1){
            	timePeriods.get(indexOf).increaseFrequency(timePeriod.getFrequency());
            }else{
            	timePeriods.add(timePeriod);
            }
            
			HotItem weekDay = new HotItem(DateTimeUtil.getWeekDay(talk.getTime()),talk.getIwords().size());
			 if((indexOf = weekDays.indexOf(weekDay) ) != -1){
				 weekDays.get(indexOf).increaseFrequency(weekDay.getFrequency());
                }else{
                	weekDays.add(weekDay);
                }
			
			 
			 HotItem hotDay = new HotItem(DateTimeUtil.getDate(talk.getTime()),talk.getIwords().size());
			 if((indexOf = hotDays.indexOf(hotDay)) != -1){
				 hotDays.get(indexOf).increaseFrequency(hotDay.getFrequency());
			 }else{
				 hotDays.add(hotDay);
			 }
			 
		}
	}
	
	/**
	 * analyst the hot words
	 * @param talks
	 * @param hotWords
	 */
	private void analystHotWords(Talk talk,List<HotItem> hotWords){
		
			
			List<IWord> words = talk.getIwords();
			for(IWord word : words){
				
				HotItem hotWordItem = new HotItem(word.getValue(),1);
				
				int indexOf = -1;
	            if((indexOf = hotWords.indexOf(hotWordItem) ) != -1){
	            	hotWords.get(indexOf).increaseFrequency(hotWordItem.getFrequency());
	            }else if(!"".equals(hotWordItem.getName().trim())){
	            	hotWords.add(hotWordItem);
	            }
				 
			}
			
	}
	 
	
	/**
	 * analyst the person , group,dicussion dialogs
	 * @param analystResult
	 * @param resource
	 */
	private void analystDialogs(OverallAnalystResult analystResult,Resource resource) {
		
		List<String> personDialogs = new ArrayList<String>();
		String hotPerson = "";
		int temp = 0;
		Iterator<PersonDialog> dialogs = resource.getPersonDialogs().values().iterator();
		while(dialogs.hasNext()){
			
			PersonDialog dialog = dialogs.next();
			
			if(dialog.getTalks().size() > temp){
				
				temp = dialog.getTalks().size();
				hotPerson = dialog.getOtherPeople().getName();
			}
			
			personDialogs.add(dialog.getOtherPeople().getName());
		}
		
		hotPerson = hotPerson+"_"+temp;
		temp = 0;
		List<String> groupDialogs = new ArrayList<String>();
		String hotGroup = "";
		
		Iterator<GroupDialog> gDialogs = resource.getGroupDialogs().values().iterator();
		while(gDialogs.hasNext()){
			
			GroupDialog dialog = gDialogs.next();
			if(dialog.getTalks().size() > temp){
				hotGroup = dialog.getGroup().getName()+"_"+dialog.getGroup().getMembers().size();
				temp = dialog.getTalks().size();
			}
			
			groupDialogs.add(dialog.getGroup().getName());
			
		}
		
		hotGroup = hotGroup + "_" +temp;
		temp = 0;
		List<String>  dicussDialogs = new ArrayList<String>();
		String hotDicussion = "";
		Iterator<GroupDialog> dDialogs = resource.getDiscussionDialogs().values().iterator();
		while(dDialogs.hasNext()){
			
			GroupDialog dialog = dDialogs.next();
			
			if(dialog.getTalks().size() > temp){
				hotDicussion = dialog.getGroup().getName()+"_"+dialog.getGroup().getMembers().size();
				temp = dialog.getTalks().size();
			}
			dicussDialogs.add(dialog.getGroup().getName());
		}
		
		hotDicussion = hotDicussion + "_" + temp;
		
		analystResult.setDialogPersons(personDialogs);
		analystResult.setDialogGroups(groupDialogs);
		analystResult.setDialogDicussions(dicussDialogs);
		analystResult.setTopHotPersons(hotPerson);
		analystResult.setTopHotGroups(hotGroup);
		analystResult.setTopHotDicussions(hotDicussion);
		
	}

}
